package com.sailfish.springbootdemo.dao.db1;

import com.sailfish.springbootdemo.common.DB1TableName;
import com.sailfish.springbootdemo.pojo.db1.Brand;
import com.sailfish.springbootdemo.pojo.db1.PcbaCompany;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

public interface BrandDao {

    /**
     * 查询所有
     *
     * @param searchValue
     * @return
     */
    @Results(id = "brandResultMap", value = {
            @Result(column = "brand_id", property = "brandId", jdbcType = JdbcType.INTEGER),
            @Result(column = "brand_name", property = "brandName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "create_time", property = "createTime", jdbcType = JdbcType.DATETIMEOFFSET),
            @Result(column = "update_time", property = "updateTime", jdbcType = JdbcType.DATETIMEOFFSET),
            @Result(column = "sort", property = "sort", jdbcType = JdbcType.INTEGER),
            @Result(column = "status", property = "status", jdbcType = JdbcType.INTEGER),
    })
    @Select("<script>" +
            "select * from " + DB1TableName.BRAND + " where 1 = 1 " +
            "<if test='searchValue != null and searchValue != \"\" '>" +
            " and brand_name like CONCAT('%',#{searchValue},'%')" +
            "</if>" +
            " order by sort " +
            "</script>")
    List<Brand> getAllBrand(@Param("searchValue") String searchValue);

    /**
     * 根据品牌名查询
     *
     * @param brandName
     * @return
     */
    @Select("select * from " + DB1TableName.BRAND + " where brand_name = #{brandName}")
    @ResultMap(value = "brandResultMap")
    List<Brand> getBrandByBrandName(@Param("brandName") String brandName);

    /**
     * 根据id查询
     *
     * @param brandId
     * @return
     */
    @ResultMap(value = "brandResultMap")
    @Select("select * from " + DB1TableName.BRAND + " where brand_id = #{brandId}")
    Brand getBrandById(@Param("brandId") Integer brandId);

    /**
     * 判断有没有重复的
     *
     * @return
     */
    @ResultMap(value = "brandResultMap")
    @Select("<script>" +
            "select * from " + DB1TableName.BRAND + " where brand_name = #{brandName} " +
            "<if test='brandId != null and brandId != \"\" '>" +
            " and brand_id != #{brandId}" +
            "</if>" +
            "</script>")
    List<Brand> judgeUniqueBrand(@Param("brandId") Integer brandId, @Param("brandName") String brandName);

    /**
     * 删除
     *
     * @param brandId
     * @return
     */
    @Delete("delete from " + DB1TableName.BRAND + " where brand_id = #{brandId}")
    Integer deleteBrandById(@Param("brandId") Integer brandId);

    /**
     * 新增
     *
     * @param brand
     * @return
     */
    @Insert("insert into " + DB1TableName.BRAND + "(brand_name,status,sort,create_time) values " +
            "(#{brand.brandName},#{brand.status},#{brand.sort},#{brand.createTime})")
    Integer addBrand(@Param("brand") Brand brand);

    /**
     * 更新
     *
     * @param brand
     * @return
     */
    @Update("<script>" +
            "update " + DB1TableName.BRAND +
            "<set>" +
            "<if test='brand.brandName != null and brand.brandName != \"\" '>" +
            " brand_name = #{brand.brandName}," +
            "</if>" +
            "<if test='brand.status != null and brand.status != \"\" '>" +
            " status = #{brand.status}," +
            "</if>" +
            "<if test='brand.sort != null and brand.sort != \"\" '>" +
            " sort = #{brand.sort}," +
            "</if>" +
            "<if test='brand.updateTime != null '>" +
            " update_time = #{brand.updateTime}," +
            "</if>" +
            "</set>" +
            " where brand_id = #{brand.brandId}" +
            "</script>")
    Integer updateBrand(@Param("brand") Brand brand);
}
